package sg.nus.iss.vmcs.controllers;

import sg.nus.iss.vmcs.gui.MachinerySimulatorPanel;
import sg.nus.iss.vmcs.gui.MessageDialog;
import sg.nus.iss.vmcs.gui.SimulatorControlPanel;
import sg.nus.iss.vmcs.models.Coin;
import sg.nus.iss.vmcs.models.Door;

/**
 * Machinery Controller for controlling the Machinery Simulator Panel
 * Added by Philip to ensure conformance to Design Model Report. 
 * Current Machinery Simulator methods are in the Store Controller.
 * 
 * @author Philip Chew
 */
public class MachineryController {
	
	/** Main Controller, Controller of this Class */
	private MainController mctrl;
	
	/** Simulator Control Panel, UI managed by this Controller */
	private MachinerySimulatorPanel msp = null;

	/** Door Object for the Machinery Simulator Panel */
	private Door door;
	
	/**
	 * Creates an instance of the Machinery Controller
	 * @param mctrl
	 */
	public MachineryController(MainController mctrl) {
		this.mctrl = mctrl;
	}
	
	/**
	 * Initializes all components managed by the Machinery Controller
	 */
	public void initialize() {
		door = new Door();
	}
	
	/**
	 * Returns the reference to the MainController
	 * @return MainController
	 */
	public MainController getMainController() {
		return mctrl;
	}
	
	/**
	 * Displays the Machinery Simulator Panel. 
	 */
    public void displayMachineryPanel(){
    	msp = new MachinerySimulatorPanel(mctrl.getSimulatorControlPanel(), mctrl.getStoreController());
        msp.display();
    }
    
    /**
     * Closes down the Machinery of the Vending Machine
     */
    public void closeDown() {
    	if(msp == null) return;
    	
    	if(isDoorClosed()) {
    		msp.closeDown();
    		mctrl.getSimulatorControlPanel().setActive(SimulatorControlPanel.ACT_MACHINERY, true);
    	}
    	else {
    		new MessageDialog(msp, "Please Lock the Door before You Leave");
    	}
    }
    
    /**
     * Gets whether the door is open or closed
     * @return True if closed, false if otherwise
     */
    public boolean isDoorClosed() {
    	return door.isDoorClosed();
    }
    
    /**
     * Sets the door object's status to locked or unlocked
     * @param isClosed True to lock, false if otherwise
     */
    public void setDoorStatus(boolean isClosed) {
    	if(mctrl.getMaintenanceController().getAccessManager().getLoginState()) {
    		door.setState(isClosed);
    	}
    	else {
    		door.setState(true);
    	}
    	
    	if(msp != null) {
    		msp.setDoorState(door.isDoorClosed());
    	}
    }
    
    /**
     * Displays the current door status, open or closed on the door status display
     */
    public void displayDoorStatus() {
    	msp.setDoorState(door.isDoorClosed());
    }
    
    /**
	 * Instructs the Cash Store to store the coin sent as input and updates the display on the machinery simulator panel
	 * @param coin Coin to store
	 * @return True if successful, false if otherwise
	 */
	public boolean storeCoin(Coin coin) {
		return this.getMainController().getStoreController().storeCoin(coin);
	}
    
	/**
	 * Instructs the drink store to dispense one drink then updates the machinery simulator panel
	 * @param drinkBrand Brand of drink to dispense
	 * @return True if dispensing if successful, false if otherwise
	 */
	public boolean dispenseDrink(int drinkBrand) {
		return this.getMainController().getStoreController().dispenseDrink(drinkBrand);
	}
    
	/**
	 * Instructs the Cash Store to issue a number of coins of a specific denomination
	 * @param coinIndex Index of coin denomination to return
	 * @param numCoins Number of coins to return
	 * @return True if change is given, false if otherwise
	 */
	public boolean giveChange(int coinIndex, int numCoins) {
		return false;
	}
	
	/**
	 * Gets an instance of the Machinery Controller Panel
	 * @return
	 */
	public MachinerySimulatorPanel getMachinerySimulatorPanel() {
		return msp;
	}
}
